/**
 * losenote.com Inc.
 * Copyright (c) 2013-2013 All Rights Reserved.
 */
package com.losenote.core.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

import com.losenote.core.QuestionComponent;
import com.losenote.enums.QuestionTypeEnum;
import com.losenote.model.Question;
import com.losenote.model.ct.QuestionConvertor;
import com.losenote.orm.QuestionDO;
import com.losenote.orm.QuestionDODAO;
import com.losenote.orm.QuestionDOExample;

/**
 * 问题操作组件
 * 
 * @author danjingyu
 * @version $Id: QuestionComponentImpl.java, v 0.1 2013-10-28 下午10:28:19 danjingyu Exp $
 */
public class QuestionComponentImpl implements QuestionComponent {
    /**quesiont操作**/
    @Autowired
    private QuestionDODAO questionDODAO;

    @SuppressWarnings("unchecked")
    @Override
    public List<Question> queryByTypeAndId(int id, QuestionTypeEnum type) {

        QuestionDOExample examp = new QuestionDOExample();
        examp.createCriteria().andRelIdEqualTo(id).andRelTypeEqualTo(type.getValue());
        List<QuestionDO> questionD0s = questionDODAO.selectByExample(examp);
        return convertList(questionD0s);
    }

    /**
     * 
     * @see com.losenote.core.QuestionComponent#queryById(int)
     */
    @Override
    public Question queryById(int id) {
        QuestionDO questionDO = questionDODAO.selectByPrimaryKey(id);
        return QuestionConvertor.fromDO(questionDO);
    }

    /**
     * 批量转换
     * 
     * @param questionD0s
     * @return
     */
    private List<Question> convertList(List<QuestionDO> questionD0s) {
        List<Question> questions = new ArrayList<Question>();
        if (CollectionUtils.isEmpty(questionD0s)) {
            return questions;
        }
        for (QuestionDO questionDO : questionD0s) {
            questions.add(QuestionConvertor.fromDO(questionDO));
        }
        return questions;
    }

    public void setQuestionDODAO(QuestionDODAO questionDODAO) {
        this.questionDODAO = questionDODAO;
    }

}
